package com.hien.starting;

import java.util.ArrayList;

import com.hien.controler.FileHandle;
import com.hien.model.TricksPranks;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class TipsActivity extends Activity implements OnClickListener {
	private TextView tv_title, tv_body;
	private ArrayList<TricksPranks> list = null;
	private FileHandle file;
	private int currentTipsIndex = 0;
	ImageButton bt_next,bt_back;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tips_layout);
		populate();
	}

	private void populate() {
		tv_title = (TextView) findViewById(R.id.tv_prank_title);
		tv_body = (TextView) findViewById(R.id.tv_prank_body);
		file = new FileHandle();
		String content = file.readingFromFile(this);
		list  = file.getItems(content);
		if(list!=null){
			Log.i("TipsActivity.populate()", "hien----> list length = " + list.size());
			
			populateTips(0);
		}
		bt_back = (ImageButton) findViewById(R.id.bt_back);
		bt_next = (ImageButton) findViewById(R.id.bt_next);
		bt_back.setOnClickListener(this);
		bt_next.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_back:
			break;
		case R.id.bt_next:
			if(currentTipsIndex < (list.size() - 1))
			populateTips(currentTipsIndex++);
			else{
				populateTips(0);
			}
			break;
		}
	}

	private void populateTips(int index) {
		Log.i("TipsActivity.populateTips()", "hien----> current index " + index );
		tv_title.setText(list.get(index).getTitle());
		tv_body.setText(list.get(index).getBody());
		currentTipsIndex = index;
	}

}
